﻿@using System.Web.Configuration
@using LabelResource = CatHouse.Lib.Lang
@model CatHouse.Models.LoginModel

@{
    Layout = null;
    ViewBag.WebName = WebConfigurationManager.AppSettings["WebName"] ?? "PetCare";
}


@using (Html.BeginForm("Login", "Admin", FormMethod.Post, new { @class = "white-popup" }))
{
    <script src="~/Scripts/jquery.validate.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
    <script src="~/Scripts/md5hash.js"></script>

    <script>
        if (typeof jQuery == 'undefined') {
            window.location = '@Url.Action("Index", "Home")';
        }

        $(document).ready(function() {
            $(document).on('click change', '#Password, #UserName', function() {
                $('.validation-summary-valid').empty();
            });

            $('form.white-popup').on('reset', function() {
                $('.text-danger').empty();
            });

            $('form.white-popup').on('submit', function(e) {
                e.preventDefault();

                var loginModel = $('form.white-popup').serializeObject();
                loginModel.Password = MD5(loginModel.Password);

                $.ajax({
                    url: $('form.white-popup').attr('action'),
                    data: loginModel,
                    type: 'POST',
                    success: function(result) {
                        if (result == '') {
                            window.location = '@Url.Action("Index", "Home")';
                        } else {
                            $('.validation-summary-valid').html(result);
                        }
                    },
                    error: function(err) {
                        $('.validation-summary-valid').html(err.statusText);
                    }
                });
            });
        });
    </script>
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>@LabelResource.WebName</h4>
        <hr />
        @Html.ValidationSummary(false, "", new { @class = "text-danger" })
        <div class="form-group">
            <div class="col-md-12">
                @Html.LabelFor(model => model.UserEmail, htmlAttributes: new { @class = "control-label" })
            </div>
            <div class="col-md-12">
                @Html.TextBoxFor(model => model.UserEmail, new { @class = "form-control", @minlength = 10, @maxlength = 255, @placeholder = "email@abc.com" })
                @Html.ValidationMessageFor(model => model.UserEmail, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-12">
                @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label" })
            </div>
            <div class="col-md-12">
                @Html.PasswordFor(model => model.Password, new { @class = "form-control", @minlength = 10, @maxlength = 255, @placeholder = "Ít nhất 10 chữ" })
                @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-3 col-md-8">
                <input type="submit" value="@LabelResource.LoginModel_Login" class="btn btn-default" />
                <input type="reset" value="@LabelResource.LoginModel_Reset" class="btn btn-default" />
            </div>
        </div>
    </div>
}